/************************************************************/
/**
 *  全局常量
 */
const STATIC = {
  DEFAULT_CATEGORY: '默认分类',
  STATUS_PUBLISH: 'publish',
  STATUS_DRAFT: 'draft',
  META_CATEGORY: 'category',
  META_TAG: 'tag',
  ASSESS_AGREE: 'agree',
  ASSESS_DISAGREE: 'disagree'
}

/************************************************************/

/**
 * 通用工具类
 */

/**
 * 标签转字符串
 * @param tags
 * @returns {string}
 */
function tagsToString (tags) {
  let str = ''
  for (let i = 0; i < tags.length; i++) {
    str += tags[i] + ','
  }
  return str.substr(0, str.length - 1)
}

/**
 * 字符串转标签
 * @param str
 * @returns {Array}
 */
function stringToTags (str) {
  return str.toString().split(',')
}

/**
 * 跳转到指定id元素处
 * @param id 元素id
 * @param offset 偏移量
 * @param speed 速度
 */
function goAnchor (id, offset, speed) {
  let anchor = document.getElementById(id)
  let targetY = anchor.getBoundingClientRect().top
  let initialY = window.pageYOffset
  offset = offset || 0
  let position = targetY + initialY - offset
  jump(position, speed)
}

/**
 * 跳转到指定position
 * @param position
 * @param speed
 */
function jump (position, speed) {
  let distance = document.documentElement.scrollTop || document.body.scrollTop
  speed = speed || 100
  let step = position / speed
  if (position > distance) {
    smoothDown()
  } else {
    let newTotal = distance - position
    step = newTotal / speed
    smoothUp()
  }

  function smoothDown () {
    if (distance < position) {
      distance += step
      document.body.scrollTop = distance
      document.documentElement.scrollTop = distance
      setTimeout(smoothDown, 1)
    } else {
      document.body.scrollTop = position
      document.documentElement.scrollTop = position
    }
  }

  function smoothUp () {
    if (distance > position) {
      distance -= step
      document.body.scrollTop = distance
      document.documentElement.scrollTop = distance
      setTimeout(smoothUp, 1)
    } else {
      document.body.scrollTop = position
      document.documentElement.scrollTop = position
    }
  }
}

export function isvalidUsername (str) {
  const valid_map = ['admin', 'editor']
  return valid_map.indexOf(str.trim()) >= 0
}

/* 合法uri*/
export function validateURL (textval) {
  const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return urlregex.test(textval)
}

/* 小写字母*/
export function validateLowerCase (str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/* 大写字母*/
export function validateUpperCase (str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/* 大小写字母*/
export function validateAlphabets (str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * validate email
 * @param email
 * @returns {boolean}
 */
export function validateEmail (email) {
  const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return re.test(email)
}

export default {
  STATIC,
  tagsToString,
  stringToTags,
  goAnchor,
  jump,
  isvalidUsername,
  validateURL,
  validateLowerCase,
  validateUpperCase,
  validateAlphabets,
  validateEmail
}


